package com.wms.client;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.wms.business.LoginBusiness;
import com.wms.domain.PDParamPath;
import com.wms.tool.Contant;
/**
 * BuildHttpClient
 * @author YQR
 * @version 1.0 2018/3/5
 */
public class BuildHttpClient {
	private static Logger logger = LoggerFactory.getLogger(BuildHttpClient.class);

	private BuildHttpClient() {
	}	
	
	private static CloseableHttpClient HttpClient = null;
	private static HttpPost post = null;
	private static StringEntity requstEntity = null;
	private static CloseableHttpResponse response = null;
	private static HttpEntity responsEentity = null;
	private static JSONObject jsonResponsEentity = null;
	/**
	 * 
	 * @param path
	 * @param bodyParams
	 * @return jsonResponsEentity
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static JSONObject doPost(String path, String bodyParams) throws ClientProtocolException, IOException {
		try {
			// 创建客户端
			HttpClient = HttpClients.createDefault();
			// 请求行
			post = new HttpPost(Contant.getProperties().get("pd_URL") + path.toString());
			// 请求头 logger.info("打印出来使用的token:\t",PDParamPath.token.toString());
			post.addHeader("token",Contant.getProperties().getProperty("token"));
			
			// 请求实体
			requstEntity = new StringEntity(bodyParams);		
			requstEntity.setContentEncoding("UTF-8");
			requstEntity.setContentType("application/json");// 发送json数据需要设置contentType
			post.setEntity(requstEntity);
			logger.info("这里是请求行 {}",post);
			logger.info("这里是请求实体 {}",bodyParams);
			// 响应
			response = HttpClient.execute(post);			
			if (response.getStatusLine().getStatusCode() == 200) {
				responsEentity = response.getEntity();
				if(responsEentity != null){
				String stringResponsEentity = EntityUtils.toString(responsEentity, "UTF-8");
				jsonResponsEentity = JSONObject.parseObject(stringResponsEentity);
				
				}
			} else {
				JSONObject reqFail = new JSONObject();
				reqFail.put("code", response.getStatusLine().getStatusCode());
				reqFail.put("failbody", response.getStatusLine());
				logger.debug("关闭会话：" + reqFail);
				return reqFail;
			}			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonResponsEentity;
	}
	public static void closeClient() throws IOException{
		logger.info("开始进行==============测试结束开始关闭客户端=============");
		HttpClient.close();
	}
}
